//
//  Tool+PsychologicalCounselling.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/7/15.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "Tool+PsychologicalCounselling.h"
#import <Hyphenate/Hyphenate.h>
#import "ChatDemoHelper.h"
#import "CustomNavViewController.h"
#import "Config.h"
#import "JHMacros.h"
#import "TagSelection.h"
@implementation Tool (PsychologicalCounselling)
+(void)setConsultantTitle:(NSArray *)array{
    [Config userDefaultsSetObject:array key:kConsultantTitle];
}
+(void)setHospitalTitle:(NSArray *)array{
    [Config userDefaultsSetObject:array key:kHospitalTitle];
}
+(void)setSkilledfieldTitle:(NSArray *)array{
    [Config userDefaultsSetObject:array key:kSkilledfieldTitle];
}

+(NSArray *)AllConsultantTitle
{
   return  [self getConfigDataByKey:kConsultantTitle];
}

+(NSArray *)AllHospitalTitle
{
    return  [self getConfigDataByKey:kHospitalTitle];
}

+(NSArray *)AllSkilledfieldTitle
{
    return  [self getConfigDataByKey:kSkilledfieldTitle];
}

+(NSArray<TagSelection*> *)getConfigDataByKey:(NSString *)key{
    NSArray *array = [Config userDefaultsGetObjectForKey:key];
    NSMutableArray *marray = [NSMutableArray new];
    for (NSDictionary *dict in array) {
        TagSelection *model = [TagSelection new];
        [model setValuesForKeysWithDictionary:dict];
        [marray addObject:model];
    }
    return marray;
}

+(void)SendMessage_AcceptOrder:(int)orderID hx_username:(NSString *)hx_username{
    [self Send:FaceToFaceAgreeApplyFor_CustomSendMessage username:hx_username ext:@{
                                                                              CustomMessageTypeKey : [self ExtCustomMessageTypeKey:FaceToFaceAgreeApplyFor_CustomSendMessage],
                                                                              CustomMessageOrderIDKey : @(orderID)}];
}
+(void)SendMessage_RefuseOrder:(int)orderID hx_username:(NSString *)hx_username{
    [self Send:FaceToFaceDontAgreeFor_CustomSendMessage username:hx_username ext:@{
                                                                                  CustomMessageTypeKey : [self ExtCustomMessageTypeKey:FaceToFaceDontAgreeFor_CustomSendMessage],
                                                                                  CustomMessageOrderIDKey : @(orderID)}];
}

+(void)SendMessage_ApplyForDiagnosisRecord:(int)orderID hx_username:(NSString *)hx_username
{
    [self Send:ApplyForDiagnosisRecord_CustomSendMessage username:hx_username ext:@{CustomMessageTypeKey : [self ExtCustomMessageTypeKey:ApplyForDiagnosisRecord_CustomSendMessage],CustomMessageOrderIDKey : @(orderID)}];
}
+(void)SendMessage_testTable:(int)orderID hx_username:(NSString *)hx_username serviceID:(int)serviceID{
    [self Send:TestTable_CustomSendMessage username:hx_username ext:@{
                                                                     CustomMessageTypeKey : [self ExtCustomMessageTypeKey:TestTable_CustomSendMessage],
                                                                     CustomMessageOrderIDKey : @(orderID),
                                                                     CustomMessageServiceIDKey : @(serviceID)}];
}

+(void)SendMessage_ServiceBegan:(int)orderID hx_username:(NSString *)hx_username serviceID:(int)serviceID
{
    [self Send:ServiceBegan_CustomSendMessage username:hx_username ext:@{
                                                                         CustomMessageTypeKey : [self ExtCustomMessageTypeKey:ServiceBegan_CustomSendMessage],
                                                                         CustomMessageOrderIDKey : @(orderID),
                                                                         CustomMessageServiceIDKey:@(serviceID)}];
}

+(void)SendMessage_ServiceEnd:(int)orderID hx_username:(NSString *)hx_username serviceID:(int)serviceID
{
    [self Send:ServiceEnd_CustomSendMessage username:hx_username ext:@{
                                                                       CustomMessageTypeKey : [self ExtCustomMessageTypeKey:ServiceEnd_CustomSendMessage],
                                                                       CustomMessageOrderIDKey : @(orderID),
                                                                       CustomMessageServiceIDKey:@(serviceID)}];
}

+(void)SendMessage_FillInDiagnosisRecord:(int)orderID hx_username:(NSString *)hx_username serviceID:(int)serviceID
{
    [self Send:FillInDiagnosisRecord_CustomSendMessage username:hx_username ext:@{
                                                                                  CustomMessageTypeKey : [self ExtCustomMessageTypeKey:FillInDiagnosisRecord_CustomSendMessage],
                                                                                  CustomMessageOrderIDKey : @(orderID),
                                                                                  CustomMessageServiceIDKey:@(serviceID)}];
}

+(void)Send:(CustomSendMessageType)type username:(NSString *)username ext:(NSDictionary *)ext
{
    EMMessage *message = [EaseSDKHelper sendTextMessage:[self TextWithType:type]
                                                     to:username
                                            messageType:EMChatTypeChat
                                             messageExt:ext];
    SendMessageViewController *chatVc = [self getCurrentChatView];
        if (chatVc && [chatVc.conversation.conversationId isEqualToString:username]) {
            [chatVc addMessageToDataSource:message progress:nil];
        }
    [[EMClient sharedClient].chatManager sendMessage:message progress:^(int progress) {
        if (chatVc && [chatVc.conversation.conversationId isEqualToString:username]){
            [chatVc.dataSource messageViewController:chatVc updateProgress:progress messageModel:nil messageBody:message.body];
            }
    } completion:^(EMMessage *Amessage, EMError *error) {
        if (chatVc && [chatVc.conversation.conversationId isEqualToString:username]){
            [chatVc _refreshAfterSentMessage:Amessage];
        }
    }];
}

+ (SendMessageViewController*)getCurrentChatView
{
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[ChatDemoHelper shareHelper].mainVC.viewControllers];
    SendMessageViewController *chatViewContrller = nil;
    for (CustomNavViewController *viewController in viewControllers)
    {
        for (id chileVC in viewController.viewControllers) {
            if ([chileVC isKindOfClass:[SendMessageViewController class]])
            {
                chatViewContrller = chileVC;
                break;
            }

        }
    }
    return chatViewContrller;
}

+(NSString *)TypeString:(OrderType)type
{
    switch (type) {
        case tuwen_OrderType:
            return @"图文";
            break;
        case yuyin_OrderType:
            return @"语音";
            break;
        case shipin_OrderType:
            return @"视频";
            break;
        case mianduimian_OrderType:
            return @"面对面";
            break;
            
        default:
            break;
    }
}

+(NSString *)TextWithType:(CustomSendMessageType)type
{
    switch (type) {
        case FaceToFaceApplyFor_CustomSendMessage:
            return @"用户预约面对面咨询";
            break;
        case FaceToFaceAgreeApplyFor_CustomSendMessage:
            return @"咨询师接受预约面对面咨询";
            break;
        case FaceToFaceDontAgreeFor_CustomSendMessage:
            return @"咨询师拒绝预约面对面咨询";
            break;
        case OrderInformation_CustomSendMessage:
            return @"用户订单支付成功";
            break;
        case ApplyForDiagnosisRecord_CustomSendMessage:
            return @"咨询师申请查看咨询履历";
            break;
        case AgreeApplyFor_CustomSendMessage:
            return @"用户同意查看咨询履历";
            break;
        case DontAgreeFor_CustomSendMessage:
            return @"用户拒绝查看咨询履历";
            break;
        case TestTable_CustomSendMessage:
            return @"咨询师指派诊前量表";
            break;
        case FillInTestTable_CustomSendMessage:
            return @"用户完成诊前量表";
            break;
        case ServiceBegan_CustomSendMessage:
            return @"咨询师开始咨询";
            break;
        case ServiceEnd_CustomSendMessage:
            return @"咨询师结束咨询";
            break;
        case UserConfirmEnd_CustomSendMessage:
            return @"用户确认咨询结束";
            break;
        case FillInDiagnosisRecord_CustomSendMessage:
            return @"咨询师完成诊断记录";
            break;
        case UserEvaluation_CustomSendMessage:
            return @"用户完成咨询评价";
            break;
        case UserConfirmsTheEnd_CustomSendMessage:
            return @"用户确认咨询结束";
            break;
        default:
            break;
    }
}

+(NSString *)ExtCustomMessageTypeKey:(CustomSendMessageType)type
{
    switch (type) {
        case FaceToFaceApplyFor_CustomSendMessage:
            return @"FaceToFaceApplyFor_CustomSendMessage";
            break;
        case FaceToFaceAgreeApplyFor_CustomSendMessage:
            return @"FaceToFaceAgreeApplyFor_CustomSendMessage";
            break;
        case FaceToFaceDontAgreeFor_CustomSendMessage:
            return @"FaceToFaceDontAgreeFor_CustomSendMessage";
            break;
        case OrderInformation_CustomSendMessage:
            return @"OrderInformation_CustomSendMessage";
            break;
        case ApplyForDiagnosisRecord_CustomSendMessage:
            return @"ApplyForDiagnosisRecord_CustomSendMessage";
            break;
        case AgreeApplyFor_CustomSendMessage:
            return @"AgreeApplyFor_CustomSendMessage";
            break;
        case DontAgreeFor_CustomSendMessage:
            return @"DontAgreeFor_CustomSendMessage";
            break;
        case TestTable_CustomSendMessage:
            return @"TestTable_CustomSendMessage";
            break;
        case FillInTestTable_CustomSendMessage:
            return @"FillInTestTable_CustomSendMessage";
            break;
        case ServiceBegan_CustomSendMessage:
            return @"ServiceBegan_CustomSendMessage";
            break;
        case ServiceEnd_CustomSendMessage:
            return @"ServiceEnd_CustomSendMessage";
            break;
        case UserConfirmEnd_CustomSendMessage:
            return @"UserConfirmEnd_CustomSendMessage";
            break;
        case FillInDiagnosisRecord_CustomSendMessage:
            return @"FillInDiagnosisRecord_CustomSendMessage";
            break;
        case UserEvaluation_CustomSendMessage:
            return @"UserEvaluation_CustomSendMessage";
            break;
        case UserConfirmsTheEnd_CustomSendMessage:
            return @"UserConfirmsTheEnd_CustomSendMessage";
            break;
        default:
            break;
    }
}

@end

